﻿using System;
using Utils.FileFolderModule;
using Utils.JsonModule;

public class PlayerDataController
{
    public PlayerData playerData = new PlayerData();
    public Setting setting = new Setting();

    public PlayerDataController()
    {
        LoadPlayerData();
        LoadSettingJson();
    }

    private void CreateNewPlayerData()
    {
        string playerDataJson = JsonHandler.GetJsonFromObject<PlayerData>(playerData);
        FileHandler.ReWriteFile(FilePathConfigue.PlayerData,playerDataJson);
    }
    
    public void LoadPlayerData()
    {
        try
        {
            playerData = JsonHandler.GetObjectFromJson<PlayerData>(FilePathConfigue.PlayerData);
            // string playerDataJson = JsonHandler.GetJsonFromObject<PlayerData>(playerData);
        }
        catch (Exception e)
        {
            CreateNewPlayerData();
        }
    }

    public void SavePlayerData()
    {
        string playerDataJson = JsonHandler.GetJsonFromObject<PlayerData>(playerData);
        FileHandler.ReWriteFile(FilePathConfigue.PlayerData, playerDataJson);
        GameController.Instance().archiveCtr.RefreshNewLevelData();
    }

    private void CreateNewSettingJson()
    {
        string settingJson = JsonHandler.GetJsonFromObject<Setting>(setting);
        FileHandler.ReWriteFile(FilePathConfigue.SettingJson, settingJson);
    }

    public void LoadSettingJson()
    {
        try
        {
            setting = JsonHandler.GetObjectFromJson<Setting>(FilePathConfigue.SettingJson);
            // string settingJson = JsonHandler.GetJsonFromObject<Setting>(setting);
            GameController.Instance().audioCtr.SetAllAudio(setting.BgMState,setting.EffectState,setting.BgMValue,setting.EffectValue);
        }
        catch (Exception e)
        {
            CreateNewSettingJson();
        }
    }

    public void SaveSettingJson(Setting mSetting)
    {
        this.setting = mSetting;
        string settingJson = JsonHandler.GetJsonFromObject<Setting>(mSetting);
        FileHandler.ReWriteFile(FilePathConfigue.SettingJson, settingJson);
    }
}